/**
 * Project		: anveshitha
 * Filename		: anveshitha.config.ConfigUtils.java
 * Author		: varun
 * Date			: Jun 15, 2008, 10:16:43 PM
 * Description	: 		
 * License		: GNU LESSER GENERAL PUBLIC LICENSE (http://www.gnu.org/licenses/lgpl-3.0.txt)
 */
package anveshitha.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author varun
 */
public class ConfigUtils {
	
	private static String LAST_INDEXED_TIME = "_lastindexedtime";
	private static Properties configProperties = new Properties();
	private static Logger log = Logger.getLogger(ConfigUtils.class);
	
	public static void writeToProperties(String key, String value) {
		try {

			configProperties.load(new FileInputStream(
					AnveshithaConstants.CONFIG_PROPERTIES));
			configProperties.put(key, value);
			configProperties.store(new FileOutputStream(
					AnveshithaConstants.CONFIG_PROPERTIES), null);
			
		} catch (FileNotFoundException fnfe) {
			log.error("File Not Found Exception: " + fnfe.getMessage(), fnfe);
		} catch (IOException ioe) {
			log.error("IO Exception: " + ioe.getMessage(), ioe);
		}
	}
	
	public static String getFromProperties(String key)
	{
		String property = null;
		try {

			configProperties.load(new FileInputStream(
					AnveshithaConstants.CONFIG_PROPERTIES));
			property = configProperties.getProperty(key);
			
		} catch (FileNotFoundException fnfe) {
			log.error("File Not Found Exception: " + fnfe.getMessage(), fnfe);
		} catch (IOException ioe) {
			log.error("IO Exception: " + ioe.getMessage(), ioe);
		}
		
		return property;
	}
	
	public static String getLastIndexedTime(String language)
	{
		String lastIndexedTime = getFromProperties(language + LAST_INDEXED_TIME);		
		return (StringUtils.isNotEmpty(lastIndexedTime)) ? lastIndexedTime : "0";
	}
	
	public static void writeLastIndexedTime(String language, long time)
	{
		writeToProperties(language + LAST_INDEXED_TIME, String.valueOf(time));
	}


}
